﻿using Assets.Scripts.UI;
using UnityEngine;

namespace Assets.Scripts.App
{
    public class InputComp:MonoBehaviour
    {
        public delegate void KeyDownHandler(object sender, KeyEventArgs e);
        public event KeyDownHandler KeyDown;
        public event KeyDownHandler KeyUp;

        private void OnGUI()
        {
            OnKeyDown();
            OnKeyUp();
        }

        void OnKeyDown()
        {
            if (Event.current.rawType == EventType.KeyDown && Event.current.keyCode != KeyCode.None)
            {
                Event.current.Use();
                var keyData = UnityKeyTranslator.ToKeyData(Event.current.modifiers, Event.current.keyCode);
                var keyArgs = new KeyEventArgs(keyData);
                KeyDown(this, keyArgs);
            }
        }
        void OnKeyUp()
        {
            if (Event.current.rawType == EventType.KeyUp && Event.current.keyCode != KeyCode.None)
            {
                Event.current.Use();
                var keyData = UnityKeyTranslator.ToKeyData(Event.current.modifiers, Event.current.keyCode);
                var keyArgs = new KeyEventArgs(keyData);
                KeyUp(this, keyArgs);
            }
        }


    }
}
